//下拉刷新
function refreshItem(param) {
    var $parent = $(param.target);
    if ($parent.find(".pull-to-refresh-layer").length !== 0) {
        return;
    }
    $parent.addClass("content pull-to-refresh-content").prepend('<div class="pull-to-refresh-layer"><div class = "preloader"> </div><div class = "pull-to-refresh-arrow"></div></div>');
    $parent.attr("isLoading", "false");
    $(document).on('refresh',param.target, function(e) {
        // 如果正在加载，则退出
        if ($parent.attr("isLoading") === "true") return;
        $parent.attr("isLoading", "true");
        var resetRefresh = function() {
            // 重置加载flag
            $parent.attr("isLoading", "false");
            $.pullToRefreshDone(param.target);
        }
        resetRefresh.prototype.type = "refresh";
        param.callBack(resetRefresh);
    });
    $.initPullToRefresh(param.target);
};
//加载更多
function addMoreItem(param) {
    var $parent = $(document).find(param.target);
    if ($parent.find(".infinite-scroll-preloader").length !== 0) {
        return;
    }
    $parent.addClass("content infinite-scroll").append('<div class="infinite-scroll-preloader hidden"><div class = "preloader"></div></div>');
    $parent.attr("isLoading", "false");

    $(document).on('infinite', param.target,function() {
        // 如果正在加载，则退出
        var $parent = $(document).find(param.target);
        if ($parent.attr("isHasDone") === "true") {
            disableAddMoreItem($parent);
            return;
        };
        if ($parent.attr("isLoading") === "true") return;
        // 设置flag
        $parent.attr("isLoading", "true");
        var resetLoading = function(noMore) {
            // 重置加载flag
            $parent.attr("isLoading", "false");
            //容器发生改变,如果是js滚动，需要刷新滚动
            $.refreshScroller();
            if (noMore) {
                disableAddMoreItem($parent);
            }
        }
        resetLoading.prototype.type = "addMore";
        param.callBack(resetLoading);
    });
    if ($parent.find(".infinite-scroll-preloader").length !==0 &&($parent.find(".infinite-scroll-preloader").offset().top > $parent.offset().height + $parent.offset().top)) {
        $parent.find(".infinite-scroll-preloader").removeClass("hidden");
    }
    $.initScroller();
    $.initInfiniteScroll($parent);
};
//
function disableAddMoreItem(target) {
    $parent = $(document).find(target);
    $infiniteScroll = $parent.find(".infinite-scroll-preloader");
    if ($infiniteScroll.length === 0) {
        return;
    }
    $infiniteScroll.addClass("hidden");
    $parent.attr("isHasDone", "true");
    $.detachInfiniteScroll && $.detachInfiniteScroll($parent);
};
//初始化更多内容提示
function initAddMoreItem(target) {
    $parent = $(document).find(target);
    $infiniteScroll = $parent.find(".infinite-scroll-preloader");
    if ($infiniteScroll.length === 0) {
        return;
    }
    $parent.attr("isHasDone", "false");
    $parent.scrollTop(0);
    $.refreshScroller();
    setTimeout(function() {
        if ($infiniteScroll.length !== 0 && $infiniteScroll.offset().top < $parent.offset().height + $parent.offset().top) {
            $infiniteScroll.addClass("hidden");
            $.detachInfiniteScroll && $.detachInfiniteScroll($parent);
        } else {
            $infiniteScroll.removeClass("hidden");
            $.attachInfiniteScroll && $.attachInfiniteScroll($parent);
        }
    }, 100);
};
function commomWaitSecond(param){
    var p=$.extend({}, param);
    var countdown=p.second?p.second:60;
    function settime(val) {
        if (countdown == 0) {
            val.removeClass("disabled");
            val.text(p.normalText?p.normalText:"获取验证码");
            countdown = p.second?p.second:60;
        } else {
            val.addClass("disabled");
            val.text(p.waiting?p.waiting(countdown):countdown + "秒后重发");
            countdown--;
            setTimeout(function() {
                settime(p.target);
            }, 1000)
        }
    }
    settime(p.target);
};
function initToolbarRefresh() {
    $(document).on('click','.common-toolbar .active',function(){
        var $this = $(this);
        if($this.attr('need-refresh')=='true'&&!$this.hasClass('refresh-now')){
            $this.addClass('refresh-now');
            $(document).trigger($this.attr('trigger-event'));
            setTimeout(function(){
                $this.removeClass('refresh-now');
            }, 1000);
        }
    })
}
